Skip to content

Conversation

arthurlw
Copy link
Member

@arthurlw arthurlw commented Aug 6, 2025

@arthurlw arthurlw changed the title BUG: Added condition for CategoricalDtype BUG: Fix all-NaY when ArrowEA.astype to categorical Aug 6, 2025
@arthurlw arthurlw changed the title BUG: Fix all-NaY when ArrowEA.astype to categorical BUG: Fix all-NaT when ArrowEA.astype to categorical Aug 6, 2025
@arthurlw
Copy link
Member Author

arthurlw commented Aug 7, 2025

Using astype with CategoricalDtype coerces values into Index, where the code tries to compare two Index objects with numpy and pyarrow types.

In the first example, the code does not think they are comparable in Index._should_compare, and returns all -1s. In the second example, the code correctly determines that they are comparable, but they try to compare it (coercing both indexes to object dtypes with Index._find_common_type_compat) and fail. Hence, both cases return all -1s.

@jbrockmendel
Copy link
Member

So we should start by fixing should_compare?

@arthurlw
Copy link
Member Author

So we should start by fixing should_compare?

Sorry I missed this earlier. The fix I pushed addresses that and resolves the second issue too.

# If we have tz, we can compare to tzaware
return isinstance(dtype, DatetimeTZDtype)

from pandas import ArrowDtype
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can go at the top of the file


return (
pa.types.is_date32(dtype.pyarrow_dtype)
or pa.types.is_date64(dtype.pyarrow_dtype)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think timestamp is comparable but date is not

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original issue was with pyarrow date dtypes, which compare fine when using astype, so I think they should be treated as comparable here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dti = pd.date_range("2016-01-01", periods=3)
item = dti[0].date()
>>> (item == dti)[0]
np.False_

We don't have a non-pyarrow date dtype, but if we did, it would not be considered comparable to datetime64

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in that case the question is whether we want astype with categoricals to succeed here, or whether astype between pyarrow date and datetime64 should be disallowed for consistency

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have analogous special-casing for the non-pyarrow dt64 that im missing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I know of

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I expect it shouldn’t be necessary here. I’ll take a closer look on monday

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the relevant special-casing is in Index._maybe_downcast_for_indexing. Take a look for the inferred_type checks

expected = array([0, 1, 2], dtype="Int64").astype("category")
tm.assert_extension_array_equal(result, expected)

def test_arrow_array_astype_to_categorical_dtype_temporal(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also test the intermediate steps that used to fail

@arthurlw arthurlw requested a review from jbrockmendel August 30, 2025 19:49
method = clean_reindex_fill_method(method)
orig_target = target
target = self._maybe_cast_listlike_indexer(target)
target, self = target._maybe_downcast_for_indexing(self)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does doing this here mean that we don't need to do it again below?


if isinstance(res, ExtensionArray):
if res_index.dtype == "string[pyarrow]" and is_timedelta64_dtype(
self.dtype
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this file we usually just check self.dtype.kind == "m"

res = keyarr
return Index(res, dtype=res.dtype)

res_index = Index(res, dtype=res.dtype)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should wrapping in an Index go just once at the end?

@arthurlw
Copy link
Member Author

arthurlw commented Sep 2, 2025

Hey @jbrockmendel, just to clarify, the deprecation in #62158 implies that date types aren’t comparable with datetime64 types. If that’s the case, then the first example in this issue is actually the expected behavior (i.e. it should return all NaTs).

arr = pd.array(
    ["2017-01-01", "2018-01-01", "2019-01-01"],
    dtype="date32[day][pyarrow]"
)
cats = pd.Index(['2017-01-01', '2018-01-01', '2019-01-01'], dtype="M8[s]")
dtype = pd.CategoricalDtype(cats, ordered=False)

arr.astype(cats.dtype)  # <- works
arr.astype(dtype)       # <- all-NaT

@jbrockmendel
Copy link
Member

The deprecation in #62158 means that the approach here won't quite work, you're right. But that deprecation won't affect the conversion of dates->datetime64 with astype, so i'd still expect date->categorical[datetime64] to work

Copy link
Contributor

github-actions bot commented Oct 4, 2025

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Oct 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: ArrowEA.astype to categorical returning all-NaT

2 participants